package cn.edu.zafu.bigdata.x.zhouyuyan.work11;

import java.sql.SQLException;
import java.util.Scanner;

public class StudentManagementSystem {
    private Database Database;
    private Scanner scanner = new Scanner(System.in);


    public void start() {
        System.out.println("======= 学生信息管理系统 =======");
        while (true) {
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    retrieveStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    System.out.println("退出程序...");
                    try {
                        Database.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    scanner.close();
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }

    private void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        Student student = new Student(id, name, phone);
        try {
            Database.addStudent(student);
            System.out.println("学生信息添加成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void retrieveStudent() {
        while (true) {
            System.out.print("请输入学号检索学生信息 (输入QUIT退出): ");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("QUIT")) {
                break;
            }

            try {
                Student student = Database.getStudentById(input);
                if (student != null) {
                    System.out.println(student);
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private void updateStudent() {
        while (true) {
            System.out.print("请输入学号更新学生信息 (输入QUIT退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            System.out.print("请输入新姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入新电话: ");
            String phone = scanner.nextLine();

            Student student = new Student(id, name, phone);
            try {
                Database.updateStudent(student);
                System.out.println("学生信息更新成功！");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private void deleteStudent() {
        while (true) {
            System.out.print("请输入学号删除学生信息 (输入QUIT退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            try {
                Database.deleteStudent(id);
                System.out.println("学生信息删除成功！");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void showMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1.添加学生信息");
            System.out.println("2.检索学生信息");
            System.out.println("3.更新学生信息");
            System.out.println("4.删除学生信息");
            System.out.println("5.退出程序");
            System.out.println("请输入操作编号（1-5）：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    retrieveStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    System.out.println("程序已退出。");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }
    public static void main(String[] args) throws SQLException {
        new StudentManagementSystem().showMenu();
    }
}

